package order.servlet;

import lombok.SneakyThrows;
import order.dao.adminDao;
import order.dao.managerDao;
import order.pojo.customer;
import order.pojo.manager;
import order.utils.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

public class managerServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
   managerDao managerDao=new managerDao();

    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=>进入了get请求");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllManager(req,resp);
            return;
        } else if("delete".equals(action)){
            deleteManagerById(req,resp);
            return;
        } else if("add".equals(action)){
            addManager(req,resp);
            return;
        } else if("update".equals(action)){
            updateManager(req,resp);
            return;
        }

    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllManager(req,resp);
            return;
        } else if("delete".equals(action)){
            deleteManagerById(req,resp);
            return;
        } else if("add".equals(action)){
          addManager(req,resp);
            return;
        } else if("update".equals(action)){
          updateManager(req,resp);
            return;
        }

    }

    //显示所有顾客
    private void querryAllManager(HttpServletRequest req,HttpServletResponse resp){
        Connection con = null;

        try {
            System.out.println("a=>进入了manager的querry的servlet");
            con = dbUtil.getCon();

            List<manager> list = managerDao.querrAllManager(con);
            System.out.println("=>"+list.toString());
            req.setAttribute("listmanager", list);
            System.out.println("a=>list"+list.toString());
//                   resp.sendRedirect("admin/managerInfo.jsp");
          req.getRequestDispatcher("admin/managerInfo.jsp").forward(req,resp);
            System.out.println("=>跳转完成");


        } catch (Exception e) {

        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    private void deleteManagerById(HttpServletRequest req,HttpServletResponse resp)  {
        System.out.println("=>进入了删除servlet");
        String managerId=req.getParameter("managerId");
        Connection con=null;
        try{
            con=dbUtil.getCon();

            managerDao.deleteManagerById(con,Integer.valueOf(managerId));
resp.sendRedirect("manager?action=querry");
//            req.getRequestDispatcher("manger?action=querry").forward(req,resp);
            System.out.println("=>完成了请求的转发");

        }catch (Exception e){

        }finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
    //修改
    private void updateManager(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        System.out.println("=>进入了修改servlet");
        manager manager=null;
        String UpmanagerId=req.getParameter("UpmanagerId");
        String managerId=req.getParameter("managerId");
        String manager_account=req.getParameter("manager_account");
        String manager_password=req.getParameter("manager_password");

//        System.out.println("UpcustomerId:"+UpcustomerId);
//        System.out.println("customerId:"+customerId);
manager=new manager(Integer.valueOf(managerId),manager_account,manager_password);

        Connection con=null;
        con=dbUtil.getCon();
        managerDao.updateManagerById(con,manager,Integer.valueOf(UpmanagerId));

        req.getRequestDispatcher("manager?action=querry").forward(req,resp);

    }
    //增加
    private void addManager(HttpServletRequest req,HttpServletResponse resp) throws Exception {


        System.out.println("=>进入了添加的servlet");
        manager manager=null;
        String managerId=req.getParameter("managerId");
        String manager_account=req.getParameter("manager_account");
        String manager_password=req.getParameter("manager_password");
        manager=new manager(Integer.valueOf(managerId),manager_account,manager_password);
        Connection con=null;
        con=dbUtil.getCon();
        managerDao.addmanager(con,manager);


        req.getRequestDispatcher("manager?action=querry").forward(req,resp);

    }






}
